package com.ln.cmsln.service.impl.wei;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ln.cmsln.mapper.wei.SuspensionMapper;
import com.ln.cmsln.pojo.entity.wei.Suspension;
import com.ln.cmsln.service.wei.SuspensionService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 魏存文
 * @since 2022-03-31
 */
@Service
public class SuspensionServiceImpl extends ServiceImpl<SuspensionMapper, Suspension> implements SuspensionService {
    /**
     * 新增悬浮框数据
     * @param suspension
     * @param realPath
     * @param file
     * @return
     */
    @Override
    public boolean add(Suspension suspension, String realPath, MultipartFile file) {
        if (suspension!=null){
            try {
                //按照时间分组存储图片
                SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd/HH/mm");
                String format = df.format(new Date());
                //组装图片存储的路径
                String imagesUrl = "\\src\\main\\resources\\image\\"+format+"\\"+file.getOriginalFilename();
                realPath=realPath+imagesUrl;
                File file2 = new File(realPath);

                //没有该路径则创建
                if (!file2.exists()){
                    file2.mkdirs();
                }
                //写入文件
                file.transferTo(file2);
                //修改图片路径
                suspension.setCode(imagesUrl);

            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
        }
        return baseMapper.insert(suspension) > 0;
    }

    /**
     * 修改悬浮框数据
     * @param suspension
     * @param realPath
     * @param file
     * @return
     */
    @Override
    public boolean updateById(Suspension suspension, String realPath, MultipartFile file) {

        if (suspension!=null){
            //获取图片相对路径
            String code = suspension.getCode();
            File file1 = new File(realPath+code);
            //删除图片
            file1.delete();
            try {
                //按照时间分组存储图片
                SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd/HH/mm");
                String format = df.format(new Date());
                //组装图片存储的路径
                String imagesUrl = "\\src\\main\\resources\\image\\"+format+"\\"+file.getOriginalFilename();
                realPath=realPath+imagesUrl;
                File file2 = new File(realPath);

                //没有该路径则创建
                if (!file2.exists()){
                    file2.mkdirs();
                }
                //写入文件
                file.transferTo(file2);
                //修改图片路径
                suspension.setCode(imagesUrl);

            }catch (Exception e){
                e.printStackTrace();
                return false;
            }
        }
        return baseMapper.updateById(suspension) > 0;
    }

    /**
     * 删除悬浮框数据
     * @param id
     * @param realPath
     * @return
     */
    @Override
    public boolean deleteById(Long id, String realPath) {
        Suspension suspension = baseMapper.selectById(id);
        //获取图片相对路径
        String code = suspension.getCode();
        File file1 = new File(realPath+code);
        //删除图片
        file1.delete();

        return baseMapper.deleteById(id) > 0;
    }


}
